﻿using ScreenMonitor.ServerLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ScreenMonitor.Common.File;
using ScreenMonitor.Common;
using MyServer.Common;

namespace ServerUI
{
    public partial class SendFileWnd : Form
    {

        private class SendFileItem
        {
            public string Path;
            public TcpClient Client;
        }

        public SendFileWnd()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = txtFilePath.Text;
            openFileDialog1.ShowDialog();
        }
        public void ReSetList()
        {
            tviewList.Nodes.Clear();
            foreach (ScreenViewerMini m in Program.ClientManager.flowLayoutPanel1.Controls)
            {
                if (m.Client.State == ClientState.Connected)
                {
                    TreeNode tnode = new TreeNode();
                    tnode.Text = m.Client.InfoStr;
                    tnode.Tag = m.Client;
                    tnode.Checked = true;
                    tviewList.Nodes.Add(tnode);
                }
            }
        }
        private void SendFileWnd_Load(object sender, EventArgs e)
        {
            ReSetList();
        }

        private void toolSelAll_Click(object sender, EventArgs e)
        {
            foreach (TreeNode t in tviewList.Nodes)
            {
                t.Checked = true;
            }
        }

        private void toolUnSelAll_Click(object sender, EventArgs e)
        {
            foreach (TreeNode t in tviewList.Nodes)
            {
                t.Checked = false;
            }
        }

        private void toolAvAll_Click(object sender, EventArgs e)
        {
            foreach (TreeNode t in tviewList.Nodes)
            {
                t.Checked = !t.Checked;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (TreeNode tnode in tviewList.Nodes)
            {
                if (tnode.Checked == true)
                {
                    ClientObject obj = (ClientObject)tnode.Tag;
                    obj.StartSendFile(txtFilePath.Text);
                    ListViewItem item = new ListViewItem(obj.Client.Client.RemoteEndPoint.ToString());
                    item.SubItems.Add("准备发送");
                    item.SubItems[1].Tag = "未知";
                    SendFileItem i = new SendFileItem();
                    i.Path = txtFilePath.Text;
                    i.Client = obj.Client;
                    item.Tag = i;
                    ListVSendQueue.Items.Add(item);
                }
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            this.txtFilePath.Text = openFileDialog1.FileName;
        }

        private void SendFileWnd_Shown(object sender, EventArgs e)
        {
            FilePool.AfterOperateRequest += FilePool_AfterOperateRequest;
        }

        private object lockobj = new object();
        void FilePool_AfterOperateRequest(FileRequestEventArgs e)
        {

            if (!e.IsCanceled && e.RequestObject.Tag != null)
            {
                if (ListVSendQueue.InvokeRequired)
                {
                    FilePool.BeforeOperateRequestEventHandler del = new FilePool.BeforeOperateRequestEventHandler(FilePool_AfterOperateRequest);
                    ListVSendQueue.BeginInvoke(del, e);
                }
                else
                {
                    lock (lockobj)
                    {
                        foreach (ListViewItem item in ListVSendQueue.Items)
                        {
                            SendFileItem i = (SendFileItem)item.Tag;
                            if (i.Client == e.RequestObject.Tag && i.Path == e.RequestObject.FilePath)
                            {
                                switch (e.RequestObject.ReauestType)
                                {
                                    case FileRequestType.Close:
                                        item.SubItems[1].Text = "已完成";
                                        break;
                                    case FileRequestType.Error:
                                        item.SubItems[1].Text = "远端出错：" + Encoding.UTF8.GetString(e.RequestObject.Buffer);
                                        break;
                                    case FileRequestType.Open:
                                        item.SubItems[1].Text = "正在请求";
                                        item.SubItems[1].Tag = BitConverter.ToInt64(e.ResponceObject.Buffer, 0);
                                        break;
                                    case FileRequestType.OpenOrCreate:
                                        item.SubItems[1].Text = "正在准备写入";
                                        item.SubItems[1].Tag = BitConverter.ToInt64(e.RequestObject.Buffer, 0);
                                        break;
                                }
                                switch (e.ResponceObject.ReauestType)
                                {
                                    case FileRequestType.Close:
                                        item.SubItems[1].Text = "已完成";
                                        break;
                                    case FileRequestType.Error:
                                        item.SubItems[1].Text = "本地出错：" + Encoding.UTF8.GetString(e.ResponceObject.Buffer);
                                        break;
                                    case FileRequestType.Read:
                                        item.SubItems[1].Text = "正在传输：" + e.ResponceObject.FileOffset.ToString() + "/" + item.SubItems[1].Tag.ToString();
                                        break;
                                    case FileRequestType.Write:
                                        item.SubItems[1].Text = "正在传输：" + e.ResponceObject.FileOffset.ToString() + "/" + item.SubItems[1].Tag.ToString();
                                        break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}